package org.dromara.Nyt7BusinessEntity.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7BusinessEntity.domain.bo.Nyt7BusinessEntityBo;
import org.dromara.Nyt7BusinessEntity.domain.vo.Nyt7BusinessEntityVo;
import org.dromara.Nyt7BusinessEntity.domain.Nyt7BusinessEntity;
import org.dromara.Nyt7BusinessEntity.mapper.Nyt7BusinessEntityMapper;
import org.dromara.Nyt7BusinessEntity.service.INyt7BusinessEntityService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 经营主体Service业务层处理
 *
 * @author Lion Li
 * @date 2025-01-08
 */
@RequiredArgsConstructor
@Service
public class Nyt7BusinessEntityServiceImpl implements INyt7BusinessEntityService {

    private final Nyt7BusinessEntityMapper baseMapper;

    /**
     * 查询经营主体
     *
     * @param id 主键
     * @return 经营主体
     */
    @Override
    public Nyt7BusinessEntityVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询经营主体列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 经营主体分页列表
     */
    @Override
    public TableDataInfo<Nyt7BusinessEntityVo> queryPageList(Nyt7BusinessEntityBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Nyt7BusinessEntity> lqw = buildQueryWrapper(bo);
        Page<Nyt7BusinessEntityVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的经营主体列表
     *
     * @param bo 查询条件
     * @return 经营主体列表
     */
    @Override
    public List<Nyt7BusinessEntityVo> queryList(Nyt7BusinessEntityBo bo) {
        LambdaQueryWrapper<Nyt7BusinessEntity> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Nyt7BusinessEntity> buildQueryWrapper(Nyt7BusinessEntityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Nyt7BusinessEntity> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getEntityName()), Nyt7BusinessEntity::getEntityName, bo.getEntityName());
        lqw.like(StringUtils.isNotBlank(bo.getLegalPersonName()), Nyt7BusinessEntity::getLegalPersonName, bo.getLegalPersonName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoneNumber()), Nyt7BusinessEntity::getPhoneNumber, bo.getPhoneNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEntityType()), Nyt7BusinessEntity::getEntityType, bo.getEntityType());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), Nyt7BusinessEntity::getLevel, bo.getLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelSource()), Nyt7BusinessEntity::getChannelSource, bo.getChannelSource());

        lqw.eq(StringUtils.isNotBlank(bo.getCity()), Nyt7BusinessEntity::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrictCounty()), Nyt7BusinessEntity::getDistrictCounty, bo.getDistrictCounty());
        lqw.eq(StringUtils.isNotBlank(bo.getCertificationStatus()), Nyt7BusinessEntity::getCertificationStatus, bo.getCertificationStatus());
        return lqw;
    }

    /**
     * 新增经营主体
     *
     * @param bo 经营主体
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Nyt7BusinessEntityBo bo) {
        Nyt7BusinessEntity add = MapstructUtils.convert(bo, Nyt7BusinessEntity.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改经营主体
     *
     * @param bo 经营主体
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Nyt7BusinessEntityBo bo) {
        Nyt7BusinessEntity update = MapstructUtils.convert(bo, Nyt7BusinessEntity.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Nyt7BusinessEntity entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除经营主体信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public boolean removeCertification(Long id) {
        baseMapper.removeCertification(id);
        return true;
    }

    @Override
    public boolean saveBatch(List<Nyt7BusinessEntity> list) {
         baseMapper.insertBatch(list);
        return true;
    }


}
